package main;

import bean.State;
import bean.Task;
import bean.Worker;
import util.exact.DecomposeWorkerStates;
import util.exact.MergeState;
import util.exact.UpdateStates;

import java.util.HashSet;
import java.util.Iterator;

public class Exact {
    public static void main(String[] args) {
        //1. 数据准备
        //工人数据
        HashSet<String> w1Skills = new HashSet<>();
        w1Skills.add("e1");
        w1Skills.add("e2");
        Worker w1 = new Worker("w1",w1Skills, 2.0,1);

        HashSet<String> w2Skills = new HashSet<>();
        w2Skills.add("e1");
        Worker w2 = new Worker("w2",w2Skills, 1.0,1);

        HashSet<String> w3Skills = new HashSet<>();
        w3Skills.add("e2");
        w3Skills.add("e3");
        Worker w3 = new Worker("w3",w3Skills, 3.0,2);

        HashSet<Worker> workers = new HashSet<>();
        workers.add(w1);
        workers.add(w2);
        workers.add(w3);

        //任务数据
        HashSet<String> taskSkills = new HashSet<>();
        taskSkills.add("e1");
        taskSkills.add("e2");
        taskSkills.add("e3");
        Task task = new Task(taskSkills);

        //计算贪心算法成本cG
        double cG = 0.0;
        HashSet<Worker> cTeam = Greedy.greedy(workers,task);
        Iterator<Worker> iteratorCTeam = cTeam.iterator();
        while ( iteratorCTeam.hasNext() ) {
            cG = cG + iteratorCTeam.next().getPayoff();
        }

        //算法第二行的state，定义为states
        HashSet<State> states = new HashSet<>();

        //2. 开始循环
        if ( workers.size() != 0 ) {
            for (Worker worker : workers) {
                if ( worker.getPayoff() < cG ) {
                    HashSet<State> tempStates = new HashSet<>();
                    /*
                    decompose方法，要考虑多种状况：
                    此处要根据worker的capacity来判断是不分解工人的state还是分解、分解几个state
                     */
                    HashSet<State> coverStates = DecomposeWorkerStates.decomposeWorkerStates(worker,task);
                    if ( coverStates.size() != 0 ) {
                        for (State stateOfWorker : coverStates) {
                            if ( states.size() != 0 ) {
                                for (State stateOfStates : states) {
                                    if ( stateOfWorker.getCost()+stateOfStates.getCost() <= cG ) {
                                        //合并状态并插入tempStates
                                        State stateTemp = MergeState.mergeState(stateOfWorker,stateOfStates);
                                        if (stateTemp != null) {
                                            tempStates.add(stateTemp);
                                        }
                                    }
                                }
                            }else { //如果此时states为空，说明当前stateOfWorker为第一个state，直接插入tempStates
                                tempStates.add(stateOfWorker);
                            }
                        }
                    }
                    //用tempState更新states
                    states = UpdateStates.updateStates(tempStates,states);

                }

            }
        }

    }
}
